package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Profile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Members;
import com.ruoyi.system.service.IMembersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 系统会员信息Controller
 * 
 * @author 陈斌斌
 * @date 2025-09-17
 */
@RestController
@RequestMapping("/system/members")
@RequiredArgsConstructor
@Profile("druid")
public class MembersController extends BaseController
{
    private final IMembersService membersService;

    /**
     * 查询系统会员信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:members:list')")
    @GetMapping("/list")
    public TableDataInfo list(Members members)
    {
        startPage();
        List<Members> list = membersService.selectMembersList(members);
        return getDataTable(list);
    }
    @Log(title = "会员导入管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:members:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Members> util = new ExcelUtil<Members>(Members.class);
        List<Members> membersList = util.importExcel(file.getInputStream());

        String message = membersService.importMembers(membersList, updateSupport);
        return success(message);
    }
    /**
     * 导出系统会员信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:members:export')")
    @Log(title = "系统会员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletRequest request,HttpServletResponse response, Members members)
    {
        startPage();
        List<Members> list = membersService.selectMembersList(members);
        if ("1".equals(members.getEOrp())){
            ExcelUtil<Members> util = new ExcelUtil<Members>(Members.class);
            if ("模糊".equals(members.getFullName())){
                util.hideColumn("createTime","updateTime","createBy","updateBy");
            }

            util.exportExcel(response, list, "会员信息数据");
        }
        if ("2".equals(members.getEOrp())){
             membersService.exportPdf(request,response,members.getFileName(),list);
        }

    }

    /**
     * 获取系统会员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:members:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(membersService.selectMembersById(id));
    }

    /**
     * 新增系统会员信息
     */
    @PreAuthorize("@ss.hasPermi('system:members:add')")
    @Log(title = "系统会员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Members members)
    {
        return toAjax(membersService.insertMembers(members));
    }

    /**
     * 修改系统会员信息
     */
    @PreAuthorize("@ss.hasPermi('system:members:edit')")
    @Log(title = "系统会员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Members members)
    {
        return toAjax(membersService.updateMembers(members));
    }

    /**
     * 删除系统会员信息
     */
    @PreAuthorize("@ss.hasPermi('system:members:remove')")
    @Log(title = "系统会员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids)
    {
        return toAjax(membersService.deleteMembersById(ids));
    }
}
